<template>
    <div>
        {{info}}
        <table class="table table-bordered" style="background-color:dodgerblue;">
            <h1  style="background-color:dodgerblue;">学生信息添加</h1>
            <tbody>
                <tr>
                    <td>学生编号</td>
                    <td><input type="text" v-model="info.idnum"></td>
                </tr>
                 <tr>
                    <td>学生姓名</td>
                    <td><input type="text" v-model="info.name"></td>
                </tr>
                 <tr>
                    <td>学生英文名称</td>
                    <td><input type="text" v-model="info.ename"></td>
                </tr>
                  <tr>
                    <td>学生手机号</td>
                    <td><input type="text" v-model="info.tel"></td>
                </tr>
                  <tr>
                    <td>学生身份证</td>
                    <td><input type="text" v-model="info.card"></td>
                </tr>
                  <tr>
                    <td>学生年龄</td>
                    <td><input type="text" v-model="info.age"></td>
                </tr>
                  <tr>
                    <td>学生出生日期</td>
                    <td><input type="date" v-model="info.birthday"></td>
                </tr>
                <tr>
                    <td>学生身份证正面</td>
                    <td><input type="file" @change="img">
                    <img :src="info.up" width="50px" height="50px">
                    </td>
                </tr>
                 <tr>
                    <td>学生身份证反面</td>
                    <td><input type="file" @change="img2">
                    <img :src="info.on" width="50px" height="50px">
                    </td>
                </tr>
                <tr>
                    <td>学生简介</td>
                    <td><textarea v-model="info.introduce"></textarea></td>
                </tr>
                <tr>
                    <td>中考成绩总分</td>
                    <td><input type="text" v-model="info.allgrade"></td>
                </tr>
                <tr>
                    <td>上课时间</td>
                    <td><input type="time" v-model="info.start"></td>
                </tr>
                  <tr>
                    <td>下课时间</td>
                    <td><input type="time" v-model="info.end"></td>
                </tr>
                <tr>
                    <td>学院</td>
                    <td>
                          <select v-model="info.lid">
            <option value="">请选择</option>
            <option :value="item.collegeidid" v-for="item in level" :key="item.collegeidid">{{item.collegename}}</option>  
        </select>
                    </td>
                </tr>
                <tr>
                    <td>班级</td>
                    <td>
                        <select v-model="info.cid">
            <option value="">请选择</option>
            <option :value="item.classid" v-for="item in level2" :key="item.classid">{{item.classname}}</option>  
        </select>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref} from 'vue'
import { onMounted } from 'vue';
onMounted(()=>{
    getlevel();
    getlevel2();
})
let level:any=ref([]);
let level2:any=ref([]); 

let info:any=ref({
  "id": 0,
  "idnum": "",
  "name": "",
  "ename": "",
  "tel": "",
  "card": "",
  "age": "",
  "birthday": "",
  "up": "",
  "on": "",
  "introduce": "",
  "allgrade": "",
  "start": "",
  "end": "",
  "lid": "",
  "cid": ""
})

const getlevel=()=>{
    axios({
        url:"https://localhost:7058/api/Stu/GetLeveltwo",
        method:'get',
        
    })
    .then((res)=>{
        console.log(res)
        level.value=res.data
    })
    .catch((err)=>{
        console.log(err);
    })
}
const getlevel2=()=>{
    axios({
        url:"https://localhost:7058/api/Stu/GetLevelone",
        method:'get',
        
    })
    .then((res)=>{
        console.log(res)
        level2.value=res.data
    })
    .catch((err)=>{
        console.log(err);
    })
}
const add=()=>{
    if(info.value.idnum==""){
        alert("学生编号不能为空!");
        return;
    }
    const na=/^[\u4e00-\u9fa5]{2,20}$/
    if(!na.test(String(info.value.name))){
        alert("学生姓名要求2-20之间的汉字");
        return;
    }
     if(info.value.name==""){
        alert("学生姓名不能为空!");
        return;
    }
    const zz=/^[a-z]{4,16}$/;
    if(!zz.test(String(info.value.ename))){
        alert("学生的英文名称必须是小写英文字母4-16位")
        return;
    }
      if(info.value.ename==""){
        alert("学生英文名称不能为空!");
        return;
    }
    const phone=/^1[3-9]\d{9}$/
    if(!phone.test(String(info.value.tel))){
        alert("学生手机号是合法的手机号");
        return;
    }
     if(info.value.tel==""){
        alert("学生手机号不能为空!");
        return;
    }
    const sfz=/^\d{17}[0-9xX]$/
    if(!sfz.test(String(info.value.card))){
        alert("学生身份证是合法的身份号")
        return;
    }
     if(info.value.card==""){
        alert("学生身份证不能为空!");
        return;
    }
  
    const nl=/^([1-9]|[1-9]\d|100)$/
    if(!nl.test(String(info.value.age))){
        alert("学生年龄需要在1-100之间")
    }
     if(info.value.age==""){
        alert("学生年龄不能为空!");
        return;
    }
     if(info.value.introduce==""){
        alert("学生简介不能为空!");
        return;
    }
     if(info.value.allgrade==""){
        alert("学生中考成绩总分不能为空!");
        return;
    }
    axios({
        url:'https://localhost:7058/api/Stu/AddStudent',
        method:'post',
        data:info.value
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert("手机号重复");
        }
        alert("添加成功");
    })
}
const img=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append('file',obj);
    axios({
        url:'https://localhost:7058/api/Img/Img',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="不是图片"){
            alert("您上传的不是图片");
            return;
        }
        if(res.data=="图片大于2M"){
            alert("上传的图片不能大于2M");
            return;
        }
        info.value.up=res.data
    })
}
const img2=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append('file',obj);
    axios({
        url:'https://localhost:7058/api/Img/Img',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="不是图片"){
            alert("您上传的不是图片");
            return;
        }
        if(res.data=="图片大于2M"){
            alert("上传的图片不能大于2M");
            return;
        }
        info.value.on=res.data
    })
}
</script>

<style scoped>

</style>